package exam.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
@Component
public class SocketServerListenHandler {

    private static MyWebSocketServer webSocketServer;
    @Autowired
    public SocketServerListenHandler(MyWebSocketServer webSocketServer) {
        this.webSocketServer = webSocketServer;
    }

    private ServerSocket serverSocket;
    static List<Socket> clientSockets;

    public SocketServerListenHandler(int port){
        try{
            serverSocket = new ServerSocket(port);
            clientSockets = new ArrayList<>();
        }catch(Exception e){
            e.printStackTrace();

        }
    }

    public void listenClientConnect(){
        while(true){
            try {
                Socket clientConnectSocket = serverSocket.accept();
                clientSockets.add(clientConnectSocket);
                new SocketServerClientHandler(clientConnectSocket).start();
                sendMessageToClients("HwcstartT");
            } catch (Exception e) {
                System.out.println("服务端监听连接程序异常");
            }
        }
    }


    // 发送消息给所有客户端的方法
    public static void sendMessageToClients(String message) {
        try {
            for (Socket socket : clientSockets) {
                new Thread(() -> {
                    try {
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(message.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 监听控制台输入，并发送消息给所有客户端
    public void listenConsoleInput() {
        try {
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                // System.out.print("请输入要发送的消息：");
                String message = consoleReader.readLine();
                sendMessageToClients(message);
                //sendMessageToWebsocket(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   /* public void InputToWeb() {
        try {
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
            while (true) {

                String message = consoleReader.readLine();
                sendMessageToWebsocket(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    */
   // 移除断开连接的客户端
  /*
   private static synchronized void removeDisconnectedClient(Socket socket) {
       if (socket != null && clientSockets.contains(socket)) {
           clientSockets.remove(socket);
           System.out.println("客户端已断开连接，已从列表中移除");
       }
   }

   */


}